package l1j.opqlo.Server;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.opqlo.List.OnlineUser;
import l1j.server.server.GMCommands;
import l1j.server.server.utils.SystemUtil;



/**
 * 指令是窗監聽器
 * @author daien
 *
 */
public class CmdEcho {

	private static final Logger _log = Logger.getLogger(CmdEcho.class.getName());
	private static final boolean _msg = false;
	private static final String _t1 = "\n\r--------------------------------------------------";
	private static final String _t2 = "\n\r--------------------------------------------------";
	/**
	 * 指令視窗監聽器
	 * @param timer
	 */
	public CmdEcho(long timer) {
		if (!_msg) {
			final String info =
					_t1
					+ (timer != 0 ? "\n       啟動指令視窗監聽器!!遊戲伺服器啟動完成!!"
							+ "\n       服務器啟動耗用時間: " + timer + "ms":"\n       目前連線帳號: " + OnlineUser.get().size())
							+ _t2;

			_log.info(info);
			SystemUtil.printMemoryUsage(_log);
		}
	}

	/**
	 * 重新啟動指令是窗監聽器
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws ClassNotFoundException
	 */
	private void reRunCmd() throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
		final CmdEcho cmdEcho = new CmdEcho(0);
		cmdEcho.runCmd();
	}

	/**
	 * 啟動指令視窗監聽器
	 * @param timer
	 * @throws ClassNotFoundException
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public void runCmd() throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
		try {

			final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			final String str = br.readLine();

			if(str != null){
				GMCommands.getInstance().handleCommands(str);
			}

			this.reRunCmd();

		} catch (final Exception e) {
			_log.log(Level.SEVERE ,e.getLocalizedMessage(), e);
		}
	}


}
